//
//  ViewController.m
//  SuiRan_Demo
//
//  Created by lanou3g on 15-4-27.
//  Copyright (c) 2015年 fzg. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, retain) UISwitch *darkSwitch;

@property (nonatomic, assign) BOOL isOpen;

@property (nonatomic, retain) UIView *mainView;

@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor cyanColor];

    [self loadViews];
    
    _isOpen = [[NSUserDefaults standardUserDefaults] boolForKey:@"switch"];
    
    NSLog(@"%d", _isOpen);
    [self.darkSwitch setOn:_isOpen animated:YES];

}


- (void)loadViews
{
    self.mainView = [[[UIView alloc] initWithFrame:self.view.bounds] autorelease];
    self.mainView.backgroundColor = [UIColor blackColor];
    
    self.mainView.alpha = 0.5;
    
    
    self.darkSwitch = [[[UISwitch alloc] initWithFrame:CGRectMake(30, 100, self.view.frame.size.width - 60, 30)] autorelease];
    
    [self.view addSubview:_darkSwitch];
    [self.darkSwitch addTarget:self action:@selector(darkSwitchAction:) forControlEvents:UIControlEventValueChanged];
}


- (void)darkSwitchAction:(UISwitch *)sender
{
    /*
//    if ([_darkSwitch isOn]) {
//        _isOpen = YES;
//        [[UIApplication sharedApplication].keyWindow addSubview:_mainView];
//        _mainView.userInteractionEnabled = NO;
//        
//        
//    } else {
//        [self.mainView removeFromSuperview];
//        _isOpen = NO;
//    }
    */
    
    if (_isOpen == YES) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dark" object:nil userInfo:@{@"isOpen": @"on"}];

        _isOpen = NO;
        
    } else if (_isOpen == NO) {

        [[NSNotificationCenter defaultCenter] postNotificationName:@"dark" object:nil userInfo:@{@"isOpen": @"off"}];

        _isOpen = YES;
    }

    [[NSUserDefaults standardUserDefaults] setBool:_isOpen forKey:@"switch"];
    [[NSUserDefaults standardUserDefaults] synchronize];
//    NSLog(@"%d", _isOpen);
//    NSLog(@"!!!!%@", _mainView);

}

/*
//- (void)viewWillDisappear:(BOOL)animated
//{
//    if (_isOpen == YES) {
//        
//        [self.mainView removeFromSuperview];
//        _isOpen = NO;
//    }
//}
//
//- (void)viewWillAppear:(BOOL)animated
//{
//    
//    if (_isOpen == YES) {
//        
//        [[UIApplication sharedApplication].keyWindow addSubview:_mainView];
//        _mainView.userInteractionEnabled = NO;
//    }
//}
*/


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
